package main

import (
	"bufio"
	"fmt"
	"learngo/functional/fib"
	"os"
)

func tryDeffer()  {
	for i:=0; i<100; i++ {
		defer fmt.Println(i)
		if i == 30 {
			// Uncomment panic to see
			// how it works with defer
			//panic("printed too many")
		}
	}
}

func writeFile(filename string) {
	//打开一个文件
	file, e := os.OpenFile(filename,
		os.O_EXCL|os.O_CREATE, 0666)

	if e !=nil {
		if pathError,ok := e.(*os.PathError); !ok{
			panic(e)
		}else{
			fmt.Println(pathError.Op)
			fmt.Println(pathError.Path)
			fmt.Println(pathError.Err)
		}

		return
	}
	defer file.Close()

	//将文件接入i/o
	writer := bufio.NewWriter(file)
	defer writer.Flush()

	f := fib.Fibonacci()

	for i := 0; i < 20; i++ {
		fmt.Fprintln(writer, f())
	}

}
func main() {
	//tryDeffer()
	writeFile("fib.txt")
}
